const Bcrypt = require("bcryptjs");
const UserModel = require("@/models/admin/user-model");
const DeptModel = require("@/models/admin/dept-model");
const RoleModel = require("@/models/admin/role-model");

/**
 * 添加用户
 */
exports.append = async (req, res, next) => {
  try {
    const current_date = new Date();
    const body_params = req.body;
    // 判断账号是否存在
    var user = await UserModel.getUserById({ username: body_params.username });
    if (user) {
      throw new Error("账号已存在");
    }
    // 判断部门
    var department = await DeptModel.getDeptById({ id: body_params.dept_id });
    if (!department) {
      throw new Error("部门不存在");
    }
    // 判断角色
    var role = RoleModel.getRoleById({ id: body_params.role_id });
    if (!role) {
      throw new Error("角色不存在");
    }

    // 初始密码 s12345
    var bcryptPassword = Bcrypt.hashSync("s12345", 10);
    // 插入数据
    const id = await UserModel.addUser({
      username: body_params.username,
      password: bcryptPassword,
      avatar: body_params.avatar,
      email: body_params.email,
      nickname: body_params.nickname,
      dept_id: body_params.dept_id,
      role_id: body_params.role_id,
      created_time: current_date,
      updated_time: current_date,
    });
    res.backSuccess(id);
  } catch (error) {
    next(error);
  }
};

/**
 * 删除用户
 */
exports.remove = async (req, res, next) => {
  try {
    const id = req.params.id;
    // 判断账号是否存在
    var user = await UserModel.getUserById({ id });
    if (!user) {
      throw new Error("账号不存在");
    }
    const result = await UserModel.deleteUser(id);
    res.backSuccess(result);
  } catch (error) {
    next(error);
  }
};

/**
 * 编辑用户
 */
exports.edit = async (req, res, next) => {
  try {
    const user_id = req.params.id;
    const body_params = req.body;

    // 判断账号是否存在
    var user = await UserModel.getUserById({ id: user_id });
    if (!user) {
      throw new Error("账号不存在");
    }
    // 判断修改的账号(usrname)是否存在
    var user_count = UserModel.getUserCount({ username: body_params.username });

    if (user_count > 1) {
      throw new Error("账号已被占用");
    }

    // 判断部门
    var department = DeptModel.getDeptById({ id: body_params.dept_id });
    if (!department) {
      throw new Error("部门不存在");
    }
    // 判断角色
    var role = RoleModel.getRoleById({ id: body_params.role_id });
    if (!role) {
      throw new Error("角色不存在");
    }
    // 更新数据
    // 需要更新的字段
    var { username, email, avatar, nickname, dept_id, role_id } = body_params;
    // 合并数据
    var togetherUser = {
      ...user,
      username,
      email,
      avatar,
      nickname,
      dept_id,
      role_id,
    };
    delete togetherUser.updated_time;
    var result = await UserModel.updateUser(user_id, togetherUser);
    res.backSuccess(result);
  } catch (error) {
    next(error);
  }
};

/**
 * 查询用户
 */
exports.page = async (req, res, next) => {
  try {
    const { username, status, page_index, page_size } = req.query;
    const result = await UserModel.getUsers({
      username,
      status,
      page_index,
      page_size
    });
    res.backSuccess(result);
  } catch (error) {
    next(error);
  }
};

/**
 * 用户详情
 */
exports.detail = async (req, res, next) => {
  try {
    const id = req.params.id;
    var user = await UserModel.getUserById({ id });
    if (!user) {
      throw new Error("账号不存在");
    }
    delete user.password;
    res.backSuccess({ ...user });
  } catch (error) {
    next(error);
  }
};

/**
 * 启用||禁用
 */
exports.status = async (req, res, next) => {
  try {
    const { id, status } = req.params;
    if (!["1", "2"].includes(status)) {
      throw new Error("状态值错误");
    }
    var user = await UserModel.getUserById({ id });

    if (!user) {
      throw new Error("账号不存在");
    }

    if (status != user.status) {
      user.status = status;
      delete user.updated_time;
      delete user.id;
      delete user.role;
      delete user.department;
      UserModel.updateUser(id, user);
    }
    res.backSuccess();
  } catch (error) {
    next(error);
  }
};
